#!/bin/bash
# This benchmark runs varying numbers of dd instances on a single file to
# read and writing to multiple output files. A basic usage is to test if
# multiple processes can read from a file quickly or if they collide in
# strange ways. A secondary use is a mixed read/write benchmark.
###SHELLPACK preamble paralleldd-bench 0
SRCTAR=
SRCTAR_EXTRA=
TARGETSIZE_MB=
NUMCPUS=$(grep -c '^processor' /proc/cpuinfo)
ITERATIONS=5
CONV=
PARALLELDD_WRITEFILES=

###SHELLPACK parseargBegin
###SHELLPACK parseargParam --sourcesize   PARALLELDD_SOURCESIZE
###SHELLPACK parseargParam --min-threads  PARALLELDD_MIN_THREADS
###SHELLPACK parseargParam --max-threads  PARALLELDD_MAX_THREADS
###SHELLPACK parseargParam --iterations   PARALLELDD_ITERATIONS
###SHELLPACK parseargYes   --write-files  PARALLELDD_WRITEFILES
###SHELLPACK parseargYes   --split-input  PARALLELDD_SPLITINPUT
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks
###SHELLPACK init_complete

if [ "$PARALLELDD_SPLITINPUT" != "yes" ]; then
	create_random_file $PARALLELDD_SOURCESIZE $SHELLPACK_DATA/source_file
	sync
fi

###SHELLPACK threads_large_stride_begin $PARALLELDD_MIN_THREADS $PARALLELDD_MAX_THREADS
	if [ "$PARALLELDD_SPLITINPUT" = "yes" ]; then
		create_random_file $((PARALLELDD_SOURCESIZE/$NR_THREADS)) $SHELLPACK_DATA/source_file.1
		for INSTANCE in `seq 2 $NR_THREADS`; do
			cp $SHELLPACK_DATA/source_file.1 $SHELLPACK_DATA/source_file.$INSTANCE
		done
		sync
	fi

	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	###SHELLPACK iteration_begin $PARALLELDD_ITERATIONS
		echo Dumping cache for cold cache read
		sync
		echo 3 > /proc/sys/vm/drop_caches

		echo Starting iteration $ITERATION/$PARALLELDD_ITERATIONS threads $NR_THREADS
		for INSTANCE in `seq 1 $NR_THREADS`; do
			OUTPUT_FILE=/dev/null
			if [ "$PARALLELDD_WRITEFILES" = "yes" ]; then
				OUTPUT_FILE=$SHELLPACK_DATA/output.$INSTANCE
			fi
			INPUT_FILE=$SHELLPACK_DATA/source_file
			if [ "$PARALLELDD_SPLITINPUT" = "yes" ]; then
				INPUT_FILE=${INPUT_FILE}.$INSTANCE
			fi
			$TIME_CMD -o $LOGDIR_RESULTS/time-$NR_THREADS-$INSTANCE-$ITERATION \
				dd if=$INPUT_FILE of=$OUTPUT_FILE 2> $LOGDIR_RESULTS/log-$NR_THREADS-$INSTANCE-$ITERATION &
		done

		echo Waiting on dd instances to complete
		wait
		rm -f $SHELLPACK_DATA/output.*
	###SHELLPACK iteration_end $PARALLELDD_ITERATIONS
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
###SHELLPACK threads_stride_end

rm $SHELLPACK_DATA/source_file*

exit $SHELLPACK_SUCCESS
